<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\exception\Jump;
use think\facade\View;
use app\admin\validate\Article_validate;
use app\admin\model\Article as ArticleModel;
class Article extends  Common{
    
    
    //构造函数
    public $article;
    public $category;
    public function  __construct() {
        parent::__construct();
    	$this->article = D('article');
    	$this->category = D('category');
    }
    
    
    // 数据列表
    public function index() {
        $list = D('article')->order('id DESC')->paginate(15);
    	return view('article/index',['list'=>$list]);
    }
    
    
     // 添加数据
    public function add() {
    	$cate = $this->category->where(['type'=>1])->field('id,catname,parentid')->order('listorder ASC')->select();
    	$cate = tree_cate($cate);
        // 	输出会员组数据集
    	$group = D('group')->field('groupid,name')->select();
    	if (request()->isPost()) {
    	    // 获取全部参数
    	    $data = input('post.');
    	     // 加载验证器
    	    $validate = new Article_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
            
            if($data['content'] == '<p><br></p>') Jump::error('内容不能为空');
    	     // 模型实例化
    		$article = ArticleModel::content_add($data);
    		// 判断是否执行成功
        	if($article) {
        		Jump::success("添加成功!!");
        	} else {
        	   Jump::error('添加失败!');
        	}
    	}
    	return view('article/content_add',['cate'=>$cate,'group' =>$group]);
    }
    
    
    
    
    
    // 修改数据
    public function edit() {
        if (request()->isPost()) {
            // 获取全部参数
    	    $data = input('post.');
    	     // 加载验证器
    	    $validate = new Article_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
            if($data['content'] == '<p><br></p>') Jump::error('内容不能为空');
    	    // 模型实例化
    		$article = ArticleModel::content_edit($data);
    		// 判断是否执行成功
        	if($article) {
        		Jump::success("修改成功!!");
        	} else {
        	   Jump::error('修改失败!');
        	}
    	}
    	
    	$id = request()->param('id');
    	$cate = $this->category->where(['type'=>1])->field('id,catname,parentid')->order('listorder ASC')->select();
    	$cate = tree_cate($cate);
    	// 	输出会员组数据集
    	$group = D('group')->field('groupid,name')->select();
        // 根据ID获取数据
    	$find = $this->article->where(['id'=>$id])->find();
    	return view('article/content_edit',['cate'=>$cate,'find'=>$find, 'group' =>$group]);
    }
    
    
    // 单个删除
    public function del() {
    	if (request()->isPost()) {
    	   $id = request()->param('id');
    		if(empty($id) ) {Jump::error('ID不存在!');}
            if($data = $this->article->where('id',$id)->delete()){
    		  Jump::success('删除成功!');    
    		}
    		else{
    		  Jump::error('删除失败!');
    		}
    		
    	}
    }
    
    
    // 移动分类
    public function remove() {
    	$cate = $this->category->where(['type'=>1])->field('id,catname,parentid')->order('listorder ASC')->select();
    	$cate = tree_cate($cate);
    	if (request()->isPost()) {
    		$ids = explode(',',input('post.ids'));
            $catid = input('post.catid');
            if(!input('post.catid')) Jump::error('缺少栏目ID!'); 
    		foreach ($ids as $id) {
    		    $data = ArticleModel::content_remove($id,$catid);
    		}
    		if($data){
    		   Jump::success('更改分类成功!'); 
    		}
    		else {
    		    Jump::error('更改分类失败!'); 
    		}
    		
    	}
    	return view('article/content_remove',['cate'=>$cate]);
    }
    
    
    
    // 复制内容
    public function copy() {
    	$cate = $this->category->where(['type'=>1])->field('id,catname,parentid')->order('listorder ASC')->select();
    	$cate = tree_cate($cate);
    	if (request()->isPost()) {
    	    $ids = explode(',',input('post.ids'));
            $catid = input('post.catid');
    	    if(!$catid) Jump::error('缺少栏目ID!'); 
    		foreach ($ids as $id) {
    		   $data = ArticleModel::content_copy($id,$catid);
    		}
    		if($data){
    		   Jump::success('复制内容成功!'); 
    		}
    		else {
    		    Jump::error('复制内容失败!'); 
    		}
    	}
    	return view('article/content_copy',['cate'=>$cate]);
    }
    
    
    
    
    
    // 属性变更
    public function operation() {
    	if (request()->isPost()) {
    	    if(!input('post.flag')) Jump::error('缺少属性ID!'); 
    	    $ids = explode(',',input('post.ids'));
    		foreach($ids as $id) {
    		    $flag = input('post.flag') && is_array(input('post.flag')) ? input('post.flag') :array(input('post.flag'));
    		    $type = input('post.type');
                // 	直接输出值
    			$data = D('article')->where('id',$id)->value('flag');
    			if($type) {
    				$new_flag = $data ? array_unique(array_merge(explode(',', $data), $flag)): $flag;
    			} else {
    				$new_flag = $data ? array_diff(explode(',', $data), $flag) : array();
    			}
    			$where = join(',', $new_flag);
    			$r = D('article')->where('id',$id)->update(array('flag'=>$where));
    		}
    		if($r){
    		   Jump::success('更改属性成功!'); 
    		}
    		else {
    		    Jump::error('更改属性失败!'); 
    		}
    		
    	}
    	return view('article/content_operation');
    }
    
    
    
    // 状态变更
    public function status_adopt() {
        if (request()->isPost()) {
            $ids = explode(',',input('post.ids'));
            if(!input('post.type'))$this -> error("没有选择变更类型!");
            foreach($ids as $id) {
    		   $r = D('article')->save(['id'=>$id,'status'=>input('post.type')]);
    		}
    		if($r){
    		   Jump::success('更改状态成功!'); 
    		}
    		else {
    		    Jump::error('更改状态失败!'); 
    		}
    	}
    	
       return view('article/adopt_operation');	
    }
    
    
    
    
    // 百度推送
	public function baidu_push() {
		if (request()->isPost()){
		    if(empty(input('post.ids'))) Jump::error('没有提交文章ID!');
			$content = D('article');
			$data = $content->field('url,is_push')->whereIn('id',[input('post.ids')])->select();
			$urls = array();
			$site_url = get_config('site_url');
			foreach ($data as $value) {
				if($value['is_push']) continue;
				$urls[] = strpos($value['url'], '://') ? $value['url'] : rtrim($site_url, '/').$value['url'];
			}

			if(!empty($urls)){
				$baidu_push_token = get_config('baidu_push_token');
				if(!$baidu_push_token) Jump::error('百度推送token为空，请到系统管理中配置！');
			    $parse_url = parse_url($site_url);
				$api_url = 'http://data.zz.baidu.com/urls?site='.$parse_url['host'].'&token='.$baidu_push_token;
				$ch = curl_init();
				$options =  array(
				    CURLOPT_URL => $api_url,
				    CURLOPT_POST => true,
				    CURLOPT_RETURNTRANSFER => true,
				    CURLOPT_POSTFIELDS => implode("\n", $urls),
				    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
				);
				curl_setopt_array($ch, $options);
				$result = curl_exec($ch);
				curl_close($ch);
				$result = json_decode($result, true);
				if(isset($result['success'])){
					$content->whereIn('id',[input('post.ids')])->update(['is_push' => 1]);
					Jump::success('成功推送'.$result['success'].'条URL地址！');
				}else{
					Jump::error('推送失败，错误码：'.$result['error']);
				}
			}
			Jump::error('没有数据被推送！');
		}
		return view('article/baidu_push');	
	}
    



    // 删除数据
    public function dels() {
    	if (request()->isPost()) {
    		$ids = explode(',',input('post.ids'));
    		if(!$ids)Jump::error('没有选择项目!!');
    		// 再循环提交过来的值
    		foreach($ids as $id) {
    			$data = D('article')->where('id',$id)->delete();
    		}
    		// 判断是否执行成功
    		if($data) {
    			Jump::success("删除成功!!");
    		} else {
    			Jump::error('删除失败!');
    		}
    	}
    	return view('article/content_dels');
    }
    
    
}